package com.itheima.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.mapper.SpecMapper;
import com.itheima.goods.pojo.Spec;
import com.itheima.goods.service.SpecService;
import entity.PageResult;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SpecServiceImpl extends ServiceImpl<SpecMapper, Spec> implements SpecService {

    @Autowired
    private SpecMapper specMapper;

    @Override
    public Result fandAll() {

        List<Spec> specData = this.list();

        return new Result(true, 200, null, specData);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result add(Spec spec) {

        Spec specId = this.specMapper.selectById(spec.getId());

        if (null != specId) {
            return new Result(false, null, "已存在相同id");
        }

        Integer count = this.specMapper.insert(spec);

        if (count == 1) {
            return new Result<Spec>(true, 200, "添加成功");
        }
        return new Result(false, null, "添加失败");
    }

    @Override
    public PageResult<Spec> findPage(Integer currentPage, Integer pageSize, String queryString) {

        Page<Spec> page = new Page<>(currentPage, pageSize);

        IPage<Spec> iPage = this.specMapper.selectPage(page, new QueryWrapper<>());

        List<Spec> pageList = iPage.getRecords();

        return new PageResult<>(iPage.getTotal(), pageList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result findById(Integer id) {

        IPage<Spec> ipage = new Page<>(1, 5);

        page(ipage, new QueryWrapper<Spec>()
                .eq(id != null, "id", id));

        List<Spec> dataList = ipage.getRecords();
        for (Spec spec : dataList) {
            if (null != spec) {
                return new Result(true, 200, "查询成功", dataList);
            }
        }
        return new Result(true, null, "查询失败，未获得数据");

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result upDateSpec(Integer id, Spec spec) {

        /*Spec spec = new Spec();

        spec.setId(id);
        spec.setName(name);
        spec.setOptions(options);
        spec.setSeq(seq);

        Integer count = this.specMapper.update(spec, new UpdateWrapper<Spec>().eq("id", id));

        if (count == 1) {
            return new Result(true, 200, "数据更新成功", null);
        }

        return new Result(false, null, "数据更新失败");*/

        boolean update = this.update(spec, new UpdateWrapper<Spec>().eq("id", id));

        if (update) {
            return new Result(true, 200, "更新成功");
        }

        return new Result(false, null, "更新失败");
    }

    @Override
    public Result deleteSpec(Integer id) {

        Integer count = this.specMapper.deleteById(id);

        if (null != count && count != 0) {
            return new Result(true, 200, "删除成功");
        }
        return new Result(true, null, "删除失败");
    }
}
